﻿namespace TestTool.GUI.Utils
{
    using iTextSharp.text;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using TestTool.GUI.Data;
    using TestTool.Tests.Definitions.Data;
    using TestTool.Tests.Definitions.Enums;
    using TestTool.Tests.Definitions.Features;
    using TestTool.Tests.Definitions.Trace;

    internal class PdfReportGenerator : PdfDocGenerator
    {
        private void AddFeaturesDefinitionLog()
        {
            base.Document.NewPage();
            Paragraph element = new Paragraph("Features Definition Log", FontFactory.GetFont("Times", (float) 16f)) {
                Alignment = 1
            };
            element.Add(Environment.NewLine);
            base.Document.Add(element);
            Paragraph paragraph2 = new Paragraph(base.Log.FeaturesDefinitionLog.ShortTextLog, FontFactory.GetFont("Times", (float) 12f));
            base.Document.Add(paragraph2);
        }

        private void AddTestGroupInfo()
        {
            base.Document.NewPage();
            Paragraph title = new Paragraph("ONVIF TEST", FontFactory.GetFont("Times", (float) 18f));
            Chapter element = new Chapter(title, 2) {
                NumberDepth = 0
            };
            Paragraph o = new Paragraph("\n\n");
            element.Add(o);
            string str = string.Empty;
            Section section = null;
            foreach (TestInfo info in from T in base.Log.TestResults.Keys
                orderby T.Category, T.Order
                select T)
            {
                string str2 = info.Group.Split(new char[] { '\\' })[0];
                if (str != str2)
                {
                    Paragraph paragraph3 = new Paragraph(string.Format("\n\n{0}", str2), FontFactory.GetFont("Times", (float) 18f));
                    section = element.AddSection(paragraph3);
                    section.NumberDepth = 0;
                    Paragraph paragraph4 = new Paragraph("\n");
                    section.Add(paragraph4);
                    str = str2;
                }
                Paragraph paragraph5 = new Paragraph();
                Anchor anchor = new Anchor(info.Name) {
                    Name = info.Name
                };
                paragraph5.Add(anchor);
                Section section2 = section.AddSection(paragraph5);
                section2.NumberDepth = 0;
                if (info.RequirementLevel == RequirementLevel.Optional)
                {
                    Paragraph paragraph6 = new Paragraph("* Optional Test");
                    section2.Add(paragraph6);
                }
                string str3 = string.Empty;
                if (base.Log.TestResults.ContainsKey(info))
                {
                    str3 = string.Format("\nTestResult\n{0}\n", base.Log.TestResults[info].ShortTextLog).Replace(string.Format("{0}\r\n", info.Name), "");
                }
                else
                {
                    str3 = "Test not run\n\n";
                }
                Paragraph paragraph7 = new Paragraph(str3, FontFactory.GetFont("Times", (float) 10f));
                section2.Add(paragraph7);
            }
            base.Document.Add(element);
        }

        private void BuildCoverPage()
        {
            Stream s = null;
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            foreach (string str in executingAssembly.GetManifestResourceNames())
            {
                if (str.EndsWith("logo.gif"))
                {
                    s = executingAssembly.GetManifestResourceStream(str);
                    break;
                }
            }
            if (s != null)
            {
                Image instance = Image.GetInstance(s);
                instance.Alignment = 1;
                base.Document.Add(instance);
            }
            base.Document.Add(new Paragraph(Environment.NewLine));
            base.Document.Add(new Paragraph(Environment.NewLine));
            base.Document.Add(new Paragraph(Environment.NewLine));
            Paragraph element = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 12f)));
            element.Add(new Chunk("ONVIF Conformance Test", FontFactory.GetFont("Times-Bold", (float) 20f)));
            element.Add("\n\n");
            element.Add(new Chunk("Performed by", FontFactory.GetFont("Times", (float) 16f)));
            element.Add("\n\n");
            element.Add(string.Format("Operator - {0}\n", base.Log.TesterInfo.Operator));
            element.Add(string.Format("Organization - {0}\n", base.Log.TesterInfo.Organization));
            element.Add(string.Format("Address - {0}\n", base.Log.TesterInfo.Address));
            element.Add(Environment.NewLine);
            element.Add(Environment.NewLine);
            element.Add(new Chunk("Device Under Test", FontFactory.GetFont("Times", (float) 16f)));
            element.Add("\n\n");
            element.Add(string.Format("Product Name - {0}\n", base.Log.ProductName));
            element.Add(string.Format("Brand - {0}\n", base.Log.DeviceInformation.Manufacturer));
            element.Add(string.Format("Model - {0}\n", base.Log.DeviceInformation.Model));
            element.Add(string.Format("Serial Number - {0}\n", base.Log.DeviceInformation.SerialNumber));
            element.Add(string.Format("Firmware Version - {0}\n", base.Log.DeviceInformation.FirmwareVersion));
            element.Add(string.Format("Other - {0}\n", base.Log.OtherInformation));
            element.Add(Environment.NewLine);
            element.Add(Environment.NewLine);
            element.Add(Environment.NewLine);
            element.Add(string.Format("{0}", base.Log.Application.ToolVersionFull));
            element.Alignment = 1;
            element.Add(Environment.NewLine);
            element.Add(Environment.NewLine);
            element.Add(Environment.NewLine);
            element.Add(string.Format("Test Date and Time - {0} @ {1}", base.Log.TestExecutionTime.ToShortDateString(), base.Log.TestExecutionTime.ToLongTimeString()));
            base.Document.Add(element);
        }

        private void BuildIndex()
        {
            base.Document.NewPage();
            Paragraph element = new Paragraph(new Chunk("Tests\n", FontFactory.GetFont("Times", (float) 16f))) {
                Alignment = 1
            };
            base.Document.Add(element);
            foreach (TestInfo info in from T in base.Log.TestResults.Keys
                orderby T.Category, T.Order
                select T)
            {
                Anchor anchor = new Anchor(string.Format("{0}\n", info.Name), FontFactory.GetFont("Times", (float) 12f)) {
                    Reference = string.Format("#{0}", info.Name)
                };
                base.Document.Add(anchor);
            }
        }

        private void BuildSummary()
        {
            base.Document.NewPage();
            int count = base.Log.TestResults.Count;
            int num2 = (from KV in base.Log.TestResults
                where KV.Value.Log.TestStatus == TestStatus.Passed
                select KV).Count<KeyValuePair<TestInfo, TestResult>>();
            int num3 = (from KV in base.Log.TestResults
                where KV.Value.Log.TestStatus == TestStatus.Failed
                select KV).Count<KeyValuePair<TestInfo, TestResult>>();
            Paragraph element = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 16f)));
            element.Add("ONVIF Test Summary\n");
            element.Alignment = 1;
            base.Document.Add(element);
            Paragraph paragraph2 = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 12f)));
            paragraph2.Add(string.Format("Tests Executed: {0}\n", count));
            paragraph2.Add(string.Format("Tests Passed:  {0}\n", num2));
            paragraph2.Add(string.Format("Tests Failed:  {0}\n", num3));
            paragraph2.Add("\n");
            base.Document.Add(paragraph2);
            base.Document.Add(new Chunk(Environment.NewLine));
            this.PrintFetures(FontFactory.GetFont("Times", (float) 12f), FontFactory.GetFont("Times", (float) 10f));
            Paragraph paragraph3 = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 12f)));
            paragraph3.Add("Timeouts: \n");
            paragraph3.Add(string.Format("Message Timeout: {0}\n", base.Log.DeviceEnvironment.Timeouts.Message));
            paragraph3.Add(string.Format("Reboot Timeout: {0}\n", base.Log.DeviceEnvironment.Timeouts.Reboot));
            paragraph3.Add(string.Format("Time between Tests: {0}\n", base.Log.DeviceEnvironment.Timeouts.InterTests));
            paragraph3.Add(string.Format("Time between Requests: {0}\n", base.Log.DeviceEnvironment.TestSettings.RecoveryDelay));
            paragraph3.Add(string.Format("Operation Delay: {0}\n", base.Log.DeviceEnvironment.TestSettings.OperationDelay));
            base.Document.Add(paragraph3);
            Paragraph paragraph4 = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 12f)));
            paragraph4.Add(string.Format("Account: {0}", base.Log.DeviceEnvironment.Credentials.UserName));
            base.Document.Add(paragraph4);
            bool flag = num3 == 0;
            Paragraph paragraph5 = new Paragraph(new Chunk("\n", FontFactory.GetFont("Times", (float) 16f)));
            if (flag)
            {
                paragraph5.Add("TEST PASSED\n");
            }
            else
            {
                paragraph5.Add("TEST FAILED\n");
            }
            paragraph5.Alignment = 1;
            base.Document.Add(paragraph5);
            this.AddFeaturesDefinitionLog();
            this.ListFailedTests();
        }

        protected override void GenerateDocumentContent()
        {
            base.Document.Footer = this.HeaderAndFooter();
            this.BuildCoverPage();
            this.BuildSummary();
            this.BuildIndex();
            this.AddTestGroupInfo();
        }

        private HeaderFooter HeaderAndFooter()
        {
            return new HeaderFooter(new Phrase(string.Format("Device - {0} {1} @ {2}  ONVIF Test Report Page: ", !string.IsNullOrEmpty(base.Log.ProductName) ? base.Log.ProductName : "<unknown>", base.Log.TestExecutionTime.ToShortDateString(), base.Log.TestExecutionTime.ToLongTimeString())), true) { Border = 0, Alignment = 1 };
        }

        private void ListFailedTests()
        {
            base.Document.NewPage();
            Paragraph element = new Paragraph(new Chunk("The following tests were FAILED:\n", FontFactory.GetFont("Times", (float) 16f))) {
                Alignment = 1
            };
            base.Document.Add(element);
            foreach (KeyValuePair<TestInfo, TestResult> pair in from T in base.Log.TestResults
                where T.Value.Log.TestStatus == TestStatus.Failed
                orderby T.Key.Category, T.Key.Order
                select T)
            {
                Paragraph paragraph2 = new Paragraph(new Chunk(pair.Key.Name, FontFactory.GetFont("Times", (float) 12f)));
                base.Document.Add(paragraph2);
            }
        }

        private void PrintFeaturesTree(Font font, List<FeatureNode> data, float indentationLeft, float step)
        {
            if (data != null)
            {
                foreach (FeatureNode node in data)
                {
                    if (base.Log.Features.Contains(node.Feature))
                    {
                        Paragraph element = new Paragraph(node.Name, font) {
                            IndentationLeft = indentationLeft
                        };
                        base.Document.Add(element);
                        if (node.Status == FeatureStatus.Group)
                        {
                            this.PrintFeaturesTree(font, node.Nodes, indentationLeft + step, step);
                        }
                    }
                }
            }
        }

        private void PrintFetures(Font titleFont, Font itemsFont)
        {
            base.Document.Add(new Paragraph(new Paragraph("Features:", titleFont)));
            FeaturesSet set = FeaturesSet.CreateFeaturesSet();
            this.PrintFeaturesTree(itemsFont, set.Nodes, 10f, 10f);
        }
    }
}

